package com.yhny.lykj.yuhannongye.app;

import android.content.SharedPreferences;
import android.text.TextUtils;

import com.longsichao.lscframe.app.LSCConfig;
import com.yhny.lykj.yuhannongye.MyApplication;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class UserConfig extends LSCConfig {
    //用户名
    private final static String CONFIG_USER_NAME = "userName";
    //用户名
    private final static String CONFIG_USER_MOBILE = "mobile";
    //密码
    private final static String CONFIG_PASS_WORD = "passWord";
    //用户id
    private final static String CONFIG_USER_ID = "userId";
    private final static String CONFIG_M_ID = "mId";
    //VIP
    private final static String CONFIG_USER_VIP = "uservip";
    //城市名称
    private final static String CONFIG_USER_CITY = "usercity";
    //城市名称
    private final static String CONFIG_USER_CITY_ID = "usercityID";
    //城市名称
    private final static String CONFIG_USER_CITY_CODE = "usercityCODE";
    //城市名称
    private final static String CONFIG_USER_PHOTO_URL = "usercityphotourl";

    private final static String KEY_SEARCH_HISTORY_KEYWORD = "key_search_history_keyword";//搜索

    //城市名称
    private final static String CITY_ID = "user_id";
    //城市ID
    private final static String CITY_NAME = "user_name";
    //所属商家编号
    private final static String SHOP_NUM = "shopNum";


    private static UserConfig appConfig = null;

    private String userName;
    private String passWord;
    private String userId;
    private String mobile;
    private String mId;
    private String userCity;
    private String userCityId;
    private String userCityCode;
    private String searchKey;
    private String photoUrl;
    private String cityId;
    private String cityName;
    private String shopNum;
    private int userVip;

    public static void initialize() {
        if (appConfig == null) {
            appConfig = new UserConfig();
        }
        SharedPreferences sp = appConfig.getSP();
        appConfig.userName = sp.getString(CONFIG_USER_NAME, "");
        appConfig.mobile = sp.getString(CONFIG_USER_MOBILE, "");
        appConfig.passWord = sp.getString(CONFIG_PASS_WORD, "");
        appConfig.userId = sp.getString(CONFIG_USER_ID, "");
        appConfig.shopNum = sp.getString(SHOP_NUM, "");
        appConfig.mId = sp.getString(CONFIG_M_ID, "");
        appConfig.userVip = sp.getInt(CONFIG_USER_VIP, 0);
        appConfig.userCity = sp.getString(CONFIG_USER_CITY, "");
        appConfig.userCityId = sp.getString(CONFIG_USER_CITY_ID, "");
        appConfig.userCityCode = sp.getString(CONFIG_USER_CITY_CODE, "");
        appConfig.searchKey = sp.getString(KEY_SEARCH_HISTORY_KEYWORD, "");
        appConfig.photoUrl = sp.getString(CONFIG_USER_PHOTO_URL, "");
        appConfig.cityId = sp.getString(CITY_ID, "");
        appConfig.cityName = sp.getString(CITY_NAME, "");


    }
    private UserConfig() {
        super(MyApplication.applicationContext);
    }

    public static int getConfigUserVip() {
        return appConfig.userVip;
    }



    /**
     * 用户名
     * @return
     */
    public static String getConfigUserName() {
        return appConfig.userName;
    }
    /**
     * 密码
     * @return
     */
    public static String getConfigPassWord() {
        return appConfig.passWord;
    }
    /**
     * 用户id
     * @return
     */
    public static String getConfigUserId() {
        if (appConfig.userId==null){
            appConfig.userId = "";
        }
        return appConfig.userId;
    }
    public static String getConfigUserMobile() {
        if (appConfig.mobile==null){
            appConfig.mobile = "";
        }
        return appConfig.mobile;
    }
    public static String getShopNum() {
        if (appConfig.shopNum==null){
            appConfig.shopNum = "";
        }
        return appConfig.shopNum;
    }
    public static String getConfigMId() {
        if (appConfig.mId==null){
            appConfig.mId = "";
        }
        return appConfig.mId;
    }
    /**
     * 城市id
     * @return
     */
    public static String getCityId() {
        if (TextUtils.isEmpty(appConfig.cityId)){
            appConfig.cityId = "";
        }
        return appConfig.cityId;
    }
    /**
     * 城市名称
     * @return
     */
    public static String getCityName() {
        if (TextUtils.isEmpty(appConfig.cityName)){
            appConfig.cityName = "";
        }
        return appConfig.cityName;
    }
    public static String getConfigUserPhotoUrl() {
        if (appConfig.photoUrl==null){
            appConfig.photoUrl = "";
        }
        return appConfig.photoUrl;
    }

    public static String getConfigUserCity() {

        if (TextUtils.isEmpty(appConfig.userCity)){
            return "北京";
        }

        return appConfig.userCity;
    }

    public static String getConfigUserCityCode() {
        if (TextUtils.isEmpty(appConfig.userCityCode)){
            return "131";
        }
        return appConfig.userCityCode;
    }

    public static String getConfigUserCityId() {
        if (TextUtils.isEmpty(appConfig.userCityId)){
            return "1";
        }
        return appConfig.userCityId;
    }

    /**
     * 清空用户数据
     */
    public static void clearUser(){
        appConfig.remove(CONFIG_USER_NAME);
        appConfig.remove(CONFIG_PASS_WORD);
        appConfig.remove(CONFIG_USER_ID);
        appConfig.remove(CONFIG_USER_VIP);
        appConfig.remove(CONFIG_USER_CITY);
        appConfig.remove(CONFIG_USER_CITY_ID);
        appConfig.remove(CONFIG_USER_CITY_CODE);
        appConfig.clear();
        appConfig = new UserConfig();
    }
    public static void setConfigUserName(String userName) {
        if (userName == null) {
            userName = "";
        }
        appConfig.userName = userName;
        appConfig.saveString(CONFIG_USER_NAME, userName);
    }

    public static void setConfigPassWord(String passWord) {
        if (passWord == null) {
            passWord = "";
        }
        appConfig.passWord = passWord;
        appConfig.saveString(CONFIG_PASS_WORD, passWord);
    }

    public static void setConfigUserId(String userId) {
        if (userId == null) {
            userId = "";
        }
        appConfig.userId = userId;
        appConfig.saveString(CONFIG_USER_ID, userId);
    }
    public static void setConfigUserMobile(String mobile) {
        if (mobile == null) {
            mobile = "";
        }
        appConfig.mobile = mobile;
        appConfig.saveString(CONFIG_USER_MOBILE, mobile);
    }

    public static void setShopNum(String shopNum) {
        if (shopNum == null) {
            shopNum = "";
        }
        appConfig.shopNum = shopNum;
        appConfig.saveString(SHOP_NUM, shopNum);
    }
    public static void setConfigMId(String mId) {
        if (mId == null) {
            mId = "";
        }
        appConfig.mId = mId;
        appConfig.saveString(CONFIG_M_ID, mId);
    }
    public static void setCityId(String cityId) {
        if (cityId == null) {
            cityId = "";
        }
        appConfig.cityId = cityId;
        appConfig.saveString(CITY_ID, cityId);
    }
    public static void setCityName(String cityName) {
        if (cityName == null) {
            cityName = "";
        }
        appConfig.cityName = cityName;
        appConfig.saveString(CITY_NAME, cityName);
    }

    public static void setConfigUserPhotoUrl(String photoUrl) {
        if (photoUrl == null) {
            photoUrl = "";
        }
        appConfig.photoUrl = photoUrl;
        appConfig.saveString(CONFIG_USER_PHOTO_URL, photoUrl);
    }

    public static void setConfigUserVip(int vip) {

        appConfig.userVip = vip;
        appConfig.saveInt(CONFIG_USER_VIP, vip);
    }

    public static void setConfigUserCity(String city) {

        appConfig.userCity = city;
        appConfig.saveString(CONFIG_USER_CITY, city);
    }

    public static void setConfigUserCityId(String cityid) {

        appConfig.userCityId = cityid;
        appConfig.saveString(CONFIG_USER_CITY_ID, cityid);
    }

    public static void setConfigUserCityCode(String cityCode) {

        appConfig.userCityCode = cityCode;
        appConfig.saveString(CONFIG_USER_CITY_CODE, cityCode);
    }

    public static void saveHistory(String text) {
        String old = appConfig.searchKey;


        if (!TextUtils.isEmpty(text) && !appConfig.searchKey.contains(text)) {
            //去除相同关键字的判断
            List<String> list = new ArrayList<String>();
            for(Object o : old.split(",")) {
                list.add((String)o);
            }
            String temoStr = "";
            if (list.size()>9){
                //控制最多保存十条
                temoStr = list.get(list.size()-1);

                appConfig.searchKey = appConfig.searchKey.substring(0,(appConfig.searchKey.length()-1-temoStr.length()));
            }
            appConfig.saveString(KEY_SEARCH_HISTORY_KEYWORD, text + "," + appConfig.searchKey);
            SharedPreferences sp = appConfig.getSP();
            appConfig.searchKey = sp.getString(KEY_SEARCH_HISTORY_KEYWORD, "");
        }
    }
    public static void cleanHistory() {
        appConfig.remove(KEY_SEARCH_HISTORY_KEYWORD);
        appConfig.searchKey = "";

    }
    public static String getSearch() {
        if (TextUtils.isEmpty(appConfig.searchKey)){
            return "";
        }
        return appConfig.searchKey;
    }

}
